import 'dart:convert';
import 'dart:io';

import 'package:flutter_comm_base_framework/core/utils/lat_lon_utils.dart';
import 'package:url_launcher/url_launcher.dart';

class MapNavUtils{
  ///高德地图地址前缀
  static String _baseAmapUrl = "${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&dev=0&style=2";
  static String _baseAmapUrl2 = "amapuri://route/plan/?dev=0&t=0";

  ///百度地图前缀
  static String _baseBaiduUrl = "baidumap://map/direction?coord_type=bd09ll&mode=driving";

  ///是否可以打开高德地图
  static Future<bool> canOpenAmap() async {
    return await canLaunch(_baseAmapUrl);
  }

  ///是否可以打开百度地图
  static Future<bool> canOpenBaidu() async {
    return await canLaunch(_baseBaiduUrl);
  }

  ///打开高德地图 02经纬度
  static gotoAmap2(double lat,double lon,{String eName}) async {
    String url = _baseAmapUrl + "&lat=$lat&lon=$lon&name=$eName";
    bool flag = await canOpenAmap();
    if(flag){
      await launch(url);
    }
  }
  static gotoAmap(double lat,double lon,{String eName}) async {
    String url = _baseAmapUrl2 + "&dlat=$lat&dlon=$lon&dname=$eName";
    bool flag = await canOpenAmap();
    if(flag){
      await launch(url);
    }
  }

  ///打开百度地图 02经纬度
  static gotoBaidu(double lat,double lon,{String eName}) async {
    List<num> latLon = LatLonUtils.gcj02_To_Bd09(lat, lon);
    String url = _baseBaiduUrl + "&destination=name:$eName|latlng:${latLon[0]},${latLon[1]}";
    bool flag = await canOpenBaidu();
    print(url);
    if(flag){
      await launch(url);
    }else{
      print("canoutOpen:$url");
    }
  }
  
}